package com.aoxin.controller.course.webCourse;

import com.aoxin.model.course.SlfChildren;
import com.aoxin.model.course.SlfClassChildren;
import com.aoxin.service.inter.course.webCourse.ClassChildrenService;
import com.aoxin.util.AjaxRes;
import com.aoxin.util.EmptyUtil;
import com.aoxin.util.enumerate.OperateType;
import com.aoxin.util.enumerate.UserType;
import com.aoxin.util.log.LogInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
    @RequestMapping("/web/classChildrenController/")
public class  ClassChildrenController {

    @Autowired
    ClassChildrenService classChildrenService;

    /**
     * 新增班级孩子关联关系
     * @param slfClassChildren
     * @param res
     * @return
     */
    @RequestMapping("insertClassChildren")
    @LogInfo(content = "班级孩子关联关系",operateType = OperateType.INSERT)
    public AjaxRes insertClassChildren(SlfClassChildren slfClassChildren, AjaxRes res){
        try {
            if(!EmptyUtil.objFieldsIsEmpty(slfClassChildren)){
                Integer result = classChildrenService.insertClassChildren(slfClassChildren);
                if(result != 0){
                    res.setSucceedMsg("insertClassChildren success!");
                }else{
                    res.setFailMsg("insertClassChildren Fail!");
                }
            }else {
                res.setFailMsg("slfClassChildren is null");
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return res;
    }


    /**
     * 修改班级孩子关联关系
     *
     * @param slfClassChildren
     * @param res
     * @return
     */
    @RequestMapping("updateClassChildren")
    @LogInfo(content = "班级孩子关联关系",operateType = OperateType.UPDATE)
    public AjaxRes updateClassChildren(SlfClassChildren slfClassChildren, AjaxRes res){
        try {
            if(!EmptyUtil.objFieldsIsEmpty(slfClassChildren)){
                Integer result = classChildrenService.updateClassChildren(slfClassChildren);
                if(result != 0){
                    res.setSucceedMsg("updateClassChildren success!");
                }else{
                    res.setFailMsg("updateClassChildren Fail!");
                }
            }else {
                res.setFailMsg("slfClassChildren is null");
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 删除班级孩子关联关系
     * @param classChildrenId
     * @param res
     * @return
     */
    @RequestMapping("deleteClassChildren")
    @LogInfo(content = "班级孩子关联关系",operateType = OperateType.DELETE,userType = UserType.USER)
    public AjaxRes deleteClassChildren(Integer classChildrenId,AjaxRes res){
        if(classChildrenId != null){
            Integer result = classChildrenService.deleteClassChildren(classChildrenId);
            if(result != 0){
                res.setSucceedMsg("deleteClassChildren success!");
            }else{
                res.setFailMsg("deleteClassChildren Fail!");
            }
        }else {
            res.setFailMsg("classChildrenId is null");
        }
        return res;
    }

    /**
     * 查询班级孩子关联关系
     * @param slfClassChildren
     * @param res
     * @return
     */
    @RequestMapping("getClassChildren")
    @LogInfo(content = "班级孩子关联关系",operateType = OperateType.QUERY)
    public AjaxRes getClassChildren(SlfClassChildren slfClassChildren, AjaxRes res){
        List<SlfClassChildren> slfClassChildrenList = classChildrenService.getClassChildren(slfClassChildren);
        res.setSucceed(slfClassChildrenList,"getClassChildren success!");
        return res;
    }

    /**
     * 班级批量插入孩子
     * @param classId
     * @param childrenIds
     * @param res
     * @return
     */
    @RequestMapping("batchInsertClassChildren")
    @LogInfo(content = "班级孩子关联关系",operateType = OperateType.INSERT)
    public AjaxRes batchInsertClassChildren(@RequestParam("classId") Integer classId, @RequestParam("childrenIds") List<Integer> childrenIds, AjaxRes res){
        if(classId != null){
            if(childrenIds != null && childrenIds.size() > 0){
                classChildrenService.batchDeleteClassChildren(classId,childrenIds);
                Integer result = classChildrenService.batchInsertClassChildren(classId,childrenIds);
                if(result != 0){
                    res.setSucceedMsg("batchInsertClassChildren success!");
                }else {
                    res.setFailMsg("batchInsertClassChildren fail!");
                }
            }else {
                res.setFailMsg("childrenIds is null");
            }
        }else {
            res.setFailMsg("classId is null");
        }
        return res;
    }


    /**
     * 通过班级id批量查询孩子
     * @param classId
     * @param res
     * @return
     */
    @RequestMapping("batchGetChildrenByClassId")
    @LogInfo(content = "班级孩子关联关系",operateType = OperateType.QUERY)
    public AjaxRes batchGetChildrenByClassId(Integer classId,AjaxRes res){
        if(classId != null){
            List<Integer> childrenIdList = classChildrenService.getChildrenIdList(classId);
            List<SlfChildren> slfChildrenList = classChildrenService.getChildrenList(childrenIdList);
            if(slfChildrenList != null && slfChildrenList.size() > 0){
                res.setSucceed(slfChildrenList,"batchGetChildrenByClassId success!");
            }else {
                res.setFailMsg("batchGetChildrenByClassId fail!");
            }
        }else {
            res.setFailMsg("classId is null");
        }
        return res;
    }


}
